package cn.clq.ymcc.web.controller;

import cn.clq.ymcc.domain.Login;
import cn.clq.ymcc.service.IMessageEmailService;
import cn.clq.ymcc.domain.MessageEmail;
import cn.clq.ymcc.query.MessageEmailQuery;
import cn.clq.ymcc.result.JSONResult;
import cn.clq.ymcc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/messageEmail")
public class MessageEmailController {

    @Autowired
    public IMessageEmailService messageEmailService;
    /**
     * @description:  课程发布成功之后，给相关用户发送邮件
     * @param: logininfo
     * @return: cn.clq.ymcc.result.JSONResult
     * @author clqlongbao
     * @date: 2022/7/26 0:02
     */
    @PostMapping("/sendEmail")
    public JSONResult sendSms(@RequestBody List<Login> logininfo){
        messageEmailService.sendEmail(logininfo);
        return JSONResult.success();
    }

    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JSONResult saveOrUpdate(@RequestBody MessageEmail messageEmail){
        if(messageEmail.getId()!=null){
            messageEmailService.updateById(messageEmail);
        }else{
            messageEmailService.insert(messageEmail);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JSONResult delete(@PathVariable("id") Long id){
        messageEmailService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @GetMapping(value = "/{id}")
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(messageEmailService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JSONResult list(){
        return JSONResult.success(messageEmailService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JSONResult page(@RequestBody MessageEmailQuery query){
        Page<MessageEmail> page = new Page<MessageEmail>(query.getPage(),query.getRows());
        page = messageEmailService.selectPage(page);
        return JSONResult.success(new PageList<MessageEmail>(page.getTotal(),page.getRecords()));
    }
}
